/**
 * 星座的controller层
 * @name: star.controller
 * @data: 2024/5/29 11:15
 */
import {Controller, Get, Inject, Param} from "@midwayjs/core";
import {StarService} from "../service/star.service";
import {Context} from "@midwayjs/koa";
import Result from "../common/Result";
import User from "../pojo/entity/user.entity";
import {HTTP_STATE_CODE} from "../common/const";
import Star from "../pojo/entity/star.entity";
import ResultInterface from "../interface/Result";

@Controller("/star")
export class StarController {
  @Inject()
  starService:StarService;
  @Inject()
  ctx: Context;


  /**
   * 获取全部星座列表,无分页
   */
  @Get("/select")
  async selectStarAll(){
    try {
      const result=await this.starService.selectAllStar();
      if (result.length>0){
        return Result<Star>({
          code: HTTP_STATE_CODE.SUCCESS,
          msg: "查询成功",
          data: result,
        });
      }else {
        return Result<User>({
          code: HTTP_STATE_CODE.NO_CONTENT,
          msg: "列表为空",
        });
      }
    }catch (e) {
      console.log(42, e);
    }
  }

  /**
   * @description 根据id查询星座
   * @param starId
   */
  @Get("/:starId")
  async selectStarById(@Param("starId") starId:number):Promise<ResultInterface<Star>>{
    try {
      const result=await this.starService.selectStarById(starId);
      if (result) {
        return Result<Star>({
          code: HTTP_STATE_CODE.SUCCESS,
          msg: "查询成功",
          data: result
        });
      }else{
        return Result<any>({
          code: HTTP_STATE_CODE.NO_CONTENT,
          msg: "查询失败",
        });
      }
    }catch (e) {
      return Result<any>({
        code: HTTP_STATE_CODE.ERROR,
        msg: "查询失败",
        data: e
      });
    }
  }

  /**
   * @description 根据name模糊查询星座列表
   * @param name
   */
  @Get("/name/:name")
  async selectStarByTitle(@Param("name") name:string):Promise<ResultInterface<Star>>{
    try {
      const result=await this.starService.selectStartByName(name);
      if (result.length>0) {
        return Result<Star>({
          code: HTTP_STATE_CODE.SUCCESS,
          msg: "查询成功",
          data: result
        });
      }else{
        return Result<any>({
          code: HTTP_STATE_CODE.NO_CONTENT,
          msg: "查询失败",
        });
      }
    }catch (e) {
      return Result<any>({
        code: HTTP_STATE_CODE.ERROR,
        msg: "查询失败",
        data: e
      });
    }
  }


}
